<?php
/*******************************************************************************
 * Project: Bridge for WordPress to phpBB 3.0.x
 * File:    widget-meta.php
 * Author:  The Greek WordPress Comunity
 * Date:    Tuesday, 1st February 2011
 * -----------------------------------------------------------------------------
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

class phpBB3Meta extends WP_Widget
{
    function phpBB3Meta()
    {
        /* Widget settings. */
        $widget_ops = array(
            'classname' => 'phpBB3 Meta Widget',
            'description' => __('Allows you to display several informations about the user.', 'wp_phpbb_bridge')
        );
        
        /* Widget control settings. */
        $control_ops = array(
            'width' => 250,
            'height' => 250,
            'id_base' => 'phpbb3-meta-widget'
        );
        
        /* Create the widget. */
        $this->WP_Widget(
            'phpbb3-meta-widget', 
            'phpBB3 Meta Widget', 
            $widget_ops, 
            $control_ops
        );
    }
    
    function form($instance)
    {
        $defaults = array(
                        'title'=>'Forum Meta',
                    );
                    
        $instance = wp_parse_args(
                        (array)$instance, 
                        $defaults
                    );
        
    ?>
    <div class="widget-content">
        <p>
            <label for="<?php echo $this->get_field_id('title') ?>">
                <?php 
                    _e('Title:', 'wp_phpbb_bridge'); 
                ?> 
            </label>
            <input style="width: 200px;" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title') ?>" type="text" value="<?php echo $instance['title']; ?>" />
        </p>
    </div>
    <?php
        echo '<input type="hidden" id="widget-wpb-meta-submit" name="widget-wpb-meta-submit" value="1" />';
    }
    
    function update($new_instance, $old_instance)
    {
        $instance = $old_instance;
        
        $instance['title'] = $new_instance['title'];
        
        return $instance;
    }
    
    function widget($args, $instance)
    {
        extract($args);

    	$options = get_option('widget_wpb_meta');
    	$title = $instance['title'];
        
    	if(empty($title))
    	{
    		$title = "&nbsp;";
    	}
    
    	echo $before_widget . $before_title . $title . $after_title;
    	widget_wpb_meta_links();
    	echo $after_widget;
    }
}

function registerMetaWidget()
{
    register_widget('phpBB3Meta');
}

add_action('widgets_init', 'registerMetaWidget');

//----------------------------------------------------------------------------------------------------


function widget_wpb_meta($args)
{
	extract($args);

	$options = get_option('widget_wpb_meta');
	$title = trim($options['title']);
	if (empty($title))
	{
		$title = "&nbsp;";
	}

	echo $before_widget . $before_title . $title . $after_title;
	widget_wpb_meta_links();
	echo $after_widget;
}

function widget_wpb_meta_links()
{
	$ucp_url = trim(get_option('wpb_url'));
	$admin_url = '';//wpb_get_admin_link();
	$mcp_url = '';//wpb_get_mcp_link();
	$permission_url = '';//wpb_get_restore_permissions_link();
?>
    <ul>
        <?php 
            if(wpb_is_user_logged_in())
            { 
        ?>
        	<li>
                <a href="<?php echo get_option('home'); ?>/wp-admin/">
                    <?php 
                        echo _e('Blog control panel', 'wp_phpbb3_bridge'); 
                    ?>
                </a>
            </li>
        	<li>
                <a href="<?php echo $ucp_url; ?>">
                    <?php 
                        echo _e('User control panel', 'wp_phpbb3_bridge'); 
                    ?>
                </a>
            </li>
            <?php 
                if(!empty($mcp_url))
                { 
            ?>
        	<li>
                <a href="<?php echo $mcp_url; ?>">
                    <?php 
                        echo _e('Moderator control panel', 'wp_phpbb3_bridge'); 
                    ?>
                </a>
            </li>
            <?php 
                } 
             
                if(!empty($admin_url))
                { 
            ?>
        	<li>
                <a href="<?php echo $admin_url; ?>">
                    <?php 
                        echo _e('Forum administration', 'wp_phpbb3_bridge'); 
                    ?>
                </a>
            </li>
            <?php
                } 
             
                if(!empty($permission_url))
                { 
            ?>
        	<li>
                <a href="<?php echo $permission_url; ?>">
                    <?php 
                        echo _e('Restore permissions', 'wp_phpbb3_bridge'); 
                    ?>
                </a>
            </li>
            <?php 
                } 
            ?>
        	<li>
                <a href="<?php echo $ucp_url; ?>?mode=logout&sid=<?php echo wpb_get_sessionid(); ?>">
                    <?php 
                        echo _e('Log out', 'wp_phpbb3_bridge'); 
                    ?>
                </a>
            </li>
        <?php
            } 
            else
            { 
        ?>
        	<li>
                <a href="<?php echo $ucp_url; ?>?mode=sendpassword">
                    <?php 
                        echo _e('I forgot my password', 'wp_phpbb3_bridge'); 
                    ?>
                </a>
            </li>
        	<li>
                <a href="<?php echo $ucp_url; ?>?mode=resend_act">
                    <?php 
                        echo _e('Resend activation email', 'wp_phpbb3_bridge'); 
                    ?>
                </a>
            </li>
        	<li>
                <a href="<?php echo $ucp_url; ?>?mode=register">
                    <?php 
                        echo _e('Register new account', 'wp_phpbb3_bridge'); 
                    ?>
                </a>
            </li>
        <?php 
            } 
        ?>
    	<li>
            <a href="<?php echo $ucp_url; ?>?mode=delete_cookies">
                <?php 
                    echo _e('Delete site cookies', 'wp_phpbb3_bridge'); 
                ?>
            </a>
        </li>
    </ul>
<?php
}

?>